//
//  WBTabBarController.swift
//  Weibo
//
//  Created by 任贤斌 on 2016/11/19.
//  Copyright © 2016年 Renkey. All rights reserved.
//

import UIKit

class WBTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //自定义tabBar
        let wbtabBar = WBTabBar()
        //只读属性不能直接赋值 可以间接赋值 kvc
        self.setValue(wbtabBar, forKey: "tabBar")

        //监听按钮的点击事件
        wbtabBar.composeClosure = { [weak self] in
            //在实际开发中 按钮点击事件处理的地方 需要留一个查找的标记
            print("撰写按钮被点击了")
        }
        
        addChildViewControllers()
    }

    //添加子视图控制器
    //private 不希望外界访问 只有本类可以
    private func addChildViewControllers() {
        addChildViewController(vc: WBHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: WBMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(vc: WBDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: WBProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //MARK: 添加子视图控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String){
        //1.实例化首页的tableVC
        //let vc = WBHomeTableViewController()
        //设置title
        //        vc.title = "首页"
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中的image
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor .orange], for: .selected)
        //设置文字大小 只能在默认状态下设置
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 10)], for: .normal)
        //设置文字和图片的间距
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
        vc.tabBarItem.title = title
        vc.navigationItem.title = title
        //2.将tableVC包装到 导航控制器
        let nav = WBBaseNavController(rootViewController: vc)
        //3.将导航控制器添加到 UITabBarController对象的子控制器
        self.addChildViewController(nav)
        
    }
}
